var bqgUtil = require('../../../../utils/biqugeUtil.js');
var WxParse = require('../../../../wxParse/wxParse.js');
var that;
Page({
  data: {
    loading: false, //是否加载中
    item: {}, //详情
    modalHidden: true,
    modalMsgHidden: true,
    isCollect: false,//是否被收藏
    content:"加载中...",
    article_id:'',
    url: ''
  },

  onLoad: function (options) {
    that = this;
    that.getContent(options.url);
  },

  prechapter: function(){
    var prevcid = that.data.item.prevcid;
    if (prevcid && prevcid != -1){
        var url = that.data.url;
        that.getContent(url.split("/").splice(0, 5).join("/") + "/" + prevcid + ".html");
    }else{
      wx.showToast({
        title: '木有更多内容了',
      })
      setTimeout(function () {
        wx.hideToast()
      }, 500)
    }
  },
  nextChapter: function(){
    var nextcid = that.data.item.nextcid;
    if (nextcid && nextcid != -1){
      var url = that.data.url;
      that.getContent(url.split("/").splice(0, 5).join("/") + "/" + nextcid + ".html");
    }else{
      wx.showToast({
        title: '木有更多内容了',
      })
      setTimeout(function () {
        wx.hideToast()
      }, 500)
    }
  },

  getContent: function(url) {
    wx.showLoading({ title: '加载中' });
    wx.request({
      url: url,
      header: { 'Content-Type': 'text/html' },
      success: function (res) {
        if (res.statusCode == 200) {
          //解析需要的参数：小说ID，章节ID，hash
          var data = bqgUtil.parseStoryContentFetchParam(res.data);
          that.setData({ article_id: data.data.article_id, url: url});
          that.ajaxload(data.data, data.data.chapter_id, false);
        }
      },
      error: function (e) {
        console.log("请求失败:" + JSON.stringify(e));
      },
      complete: function () {
      }
    });
  },

 ajaxload: function(postdata, cid, _async) {
    var data = 'id=' + postdata.article_id + '&eKey=' + postdata.hash + '&cid=' + postdata.cid + '&basecid=' + postdata.chapter_id;
    var res = '';
    var content = '';
    wx.request({
      method: 'POST',
      url: bqgUtil.getHost() + '/home/index/ajaxchapter',
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      data: data,
      async: true,
      success: function (result) {
        var data = result.data;
        if (data.status == 'success') {
          res = data.info.content;
          var info = data.info;
          if (!_async) {
            var toloadhtml = '';
            if (data.nextpage) {
              toloadhtml = '<p style="text-align:center" class="loadnextpage"><button data-nextpage="' + data.nextpage + '" data-pagehash="' + data.hash + '">本章未完，加载下一页>></button></p>';
            }
            content = res + toloadhtml;
            //预加载，暂不使用
            // if (postdata.cid == postdata.chapter_id && data.info.nextcid > -1) {
            //   postdata['cid'] = data.info.nextcid;
            //   ajaxload(postdata, data.info.nextcid, true);
            // }
            // if (postdata.cid == data.info.nextcid || data.info.nextcid == -1) {
            //   postdata['cid'] = data.info.prevcid;
            //   ajaxload(postdata, data.info.prevcid, true);
            // }
          }
          WxParse.wxParse('article', 'html', content.replace(/<br><br>/g, "<br>"), that, 5);
          that.setData({ item: info});
          wx.pageScrollTo({scrollTop: 0})
        } else {
          if (!_async) {
            console.error('<p class="preload">转码失败，请重试！<button>重新载入</button></p>');
          }
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },

  //重新加载数据
  reloadEvent: function () {
  }
});

